package com.xiaoma.listener;

import com.rabbitmq.client.Channel;
import com.xiaoma.service.OrderService;
import entity.Payorder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author: xiaoma
 * @Date: 2021/7/16 11:03
 * @description:
 */
@Component
@RabbitListener(queues = "order.release.order.queue")
@Slf4j
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public  void  listener(Payorder order, Message message, Channel channel) throws IOException {
        log.info("收到过期消息，准备关闭订单:"+order.getId());
        //手动ack，非批量模式
       try{
           orderService.closeOrder(order);
           channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
       }catch (Exception e){
           channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
       }
    }
}
